<?php

class Dispatcher extends Core {
	protected function theme_path() {
		return THEME . DS . $this->Config->theme . DS;
	}
	
	public function dispatch() {
		$request = str_replace(Mapper::base(), '', $_SERVER['REQUEST_URI']);
		if (File::exists($this->theme_path() . 'functions.php')) {
			require_once $this->theme_path() . 'functions.php';
		}
		if ($this->Config->maintenance) {
			if (File::exists($this->theme_path() . 'maintenance.php')) {
				return require_once $this->theme_path() . 'maintenance.php';
			}
		} else {
			foreach (Mapper::routes() as $key => $value) {
				if (File::exists($this->theme_path() . $value)) {
					if (preg_match('@^' . $key . '$@', Mapper::normalize($request), $_GET)) {
						return $this->View->render($value, 'template.php');
		            }
				}
			}
			return $this->View->render('404.php', 'template.php');
		}
	}
}

?>